package com.ayuku.commons.ui;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ayuku.commons.AYConst;
import com.ayuku.commons.SqliteDB;
import com.ayuku.commons.Util;

public abstract class ListWithFormMaster extends Composite {
	private Table tableData;
	private Text fSearch;
	private Label lblMessage;
	private Button btnFind;
	private String funcId;
	private Object[][] data;
	private int formMode = AYConst.MODE_EDIT;
	
	protected int rowId;
	private ToolItem btnSaveForm;
	private ToolItem btnCancelForm;
	private Composite formContainer;
	private Composite composite;
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public ListWithFormMaster(Composite parent, int style) {
		super(parent, style);

		data = new Object[10][];
		setLayout(new GridLayout(2, false));

		composite = new Composite(this, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite.setLayout(new RowLayout(SWT.HORIZONTAL));

		ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.RIGHT);

		ToolItem btnNew = new ToolItem(toolBar, SWT.NONE);
		btnNew.setImage(SWTResourceManager.getImage(ListWithFormMaster.class,
				"/images/form/database_add.png"));
		btnNew.setText("Add");

		btnNew.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				btnNewWidgetSelected(evt);
			}
		});

		ToolItem btnEdit = new ToolItem(toolBar, SWT.NONE);
		btnEdit.setImage(SWTResourceManager.getImage(ListWithFormMaster.class,
				"/images/form/database_edit.png"));
		btnEdit.setText("Edit");

		btnEdit.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				btnEditWidgetSelected(evt);
			}
		});

		ToolItem btnDelete = new ToolItem(toolBar, SWT.NONE);
		btnDelete.setImage(SWTResourceManager.getImage(ListWithFormMaster.class,
				"/images/form/database_delete.png"));
		btnDelete.setText("Delete");
		btnDelete.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				btnDeleteWidgetSelected(evt);
			}
		});
		ToolItem btnReload = new ToolItem(toolBar, SWT.NONE);
		btnReload.setImage(SWTResourceManager.getImage(ListWithFormMaster.class,
				"/images/form/database_refresh.png"));
		btnReload.setText("Refresh");
		btnReload.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				btnReloadWidgetSelected(evt);
			}
		});
		new Label(this, SWT.NONE);

		Composite composite_1 = new Composite(this, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_1.setLayout(new GridLayout(4, false));

		Label lblNewLabel = new Label(composite_1, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel.setText("Search");

		fSearch = new Text(composite_1, SWT.BORDER);
		fSearch.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		fSearch.setToolTipText("Kotak 'Pencarian' ketikkan data yang akan di cari di kotak ini kemudian tekan enter atau tekan tombol 'Pencarian'");
		fSearch.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent evt) {
				fSearchKeyReleased(evt);
			}
		});
		btnFind = new Button(composite_1, SWT.NONE);
		btnFind.setImage(SWTResourceManager.getImage(ListWithFormMaster.class,
				"/images/form/zoom.png"));
		btnFind.setText("Go");
		btnFind.setToolTipText("Tombol 'Search' tekan tombol ini untuk memulai pencarian data");
		btnFind.setText(" Go ");
		btnFind.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				btnFindWidgetSelected(evt);
			}
		});

		Button btnClear = new Button(composite_1, SWT.NONE);
		btnClear.setText("Clear");
		btnClear.setToolTipText("Tombol 'Clear search' untuk menghapus kriteria pencarian yang diketikkan di kotak pencarian dan menampilkan data tanpa kriteria.");
		btnClear.setText("Reset");
		btnClear.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				btnClearWidgetSelected(evt);
			}
		});
		new Label(this, SWT.NONE);

		setTableData(new Table(this, SWT.VIRTUAL | SWT.MULTI
				| SWT.FULL_SELECTION | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL));
		getTableData().setHeaderVisible(true);
		getTableData().setLinesVisible(true);

		tableData.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent evt) {
				tableDataKeyPressed(evt);
			}
		});

		tableData.addMouseListener(new MouseAdapter() {
			public void mouseDoubleClick(MouseEvent evt) {

				tableDataMouseDoubleClick(evt);

			}
		});
		
		formContainer = new Composite(this, SWT.BORDER);
		formContainer.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		GridData gd_formContainer = new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1);
		gd_formContainer.widthHint = 300;
		formContainer.setLayoutData(gd_formContainer);
		formContainer.setLayout(new GridLayout(1, false));
		
		//DONT REMOVE THIS
		ToolBar toolBar_1 = new ToolBar(formContainer, SWT.FLAT | SWT.RIGHT);
		toolBar_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		toolBar_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		btnSaveForm = new ToolItem(toolBar_1, SWT.NONE);
		btnSaveForm.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				btnSaveFormClick(arg0);
			}
		});
		btnSaveForm.setImage(SWTResourceManager.getImage(ListWithFormMaster.class, "/images/form/database_save.png"));
		btnSaveForm.setText("Save");
		
		btnCancelForm = new ToolItem(toolBar_1, SWT.NONE);
		btnCancelForm.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				btnCancelFormClick(arg0);
			}
		});
		btnCancelForm.setImage(SWTResourceManager.getImage(ListWithFormMaster.class, "/images/form/database_refresh.png"));
		btnCancelForm.setText("Cancel");
		
		Label label = new Label(formContainer, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		Composite compEmpty = new Composite(formContainer, SWT.NONE);
		compEmpty.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		compEmpty.setLayout(new FillLayout(SWT.HORIZONTAL));
		compEmpty.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
		
		getCompositeForm(compEmpty);
		//DON
		

		lblMessage = new Label(this, SWT.NONE);
		lblMessage.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		lblMessage.setText("Info");

		final boolean dt = createTableColumn(getTableData());
		if (dt) {

			Thread thread = new Thread() {
				public void run() {

					Display.getDefault().syncExec(new Runnable() {
						public void run() {
							try {
								System.out
										.println("*** masuk load thread data ...");
								loadDataThread();
								System.out
										.println("*** masuk load thread data akhir ...");
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});

				}
			};
			thread.start();
		}

		tableData.addListener(SWT.SetData, new Listener() {
			public void handleEvent(Event e) {
				TableItem item = (TableItem) e.item;
				int index = tableData.indexOf(item);
				Object[] datum = data[index];
				if (data.length > 0) {
					fillTable(datum, item);
				}
			}
		});

		// DON DELETE
		additionalProcess();

		// Add sort indicator and sort data when column selected
		Listener sortListener = new Listener() {
			public void handleEvent(Event e) {
				// determine new sort column and direction
				TableColumn sortColumn = tableData.getSortColumn();
				TableColumn currentColumn = (TableColumn) e.widget;
				int dir = tableData.getSortDirection();
				if (sortColumn == currentColumn) {
					dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
				} else {
					tableData.setSortColumn(currentColumn);
					dir = SWT.UP;
				}

				int s = 0;

				// sort the data based on column and direction
				for (int i = 0; i < tableData.getColumnCount(); i++) {
					if (currentColumn == tableData.getColumn(i)) {
						s = i;
					}
				}

				final int index = s;
				final int direction = dir;
				Arrays.sort(data, new Comparator<Object>() {
					public int compare(Object arg0, Object arg1) {
						Object[] a = (Object[]) arg0;
						Object[] b = (Object[]) arg1;
						if (a[index] == b[index])
							return 0;
						if (direction == SWT.UP) {

							if (a[index].getClass().equals(Integer.class)) {
								return ((Integer) a[index])
										.compareTo(((Integer) b[index])) > 0 ? -1
										: 1;
							} else if (a[index].getClass().equals(String.class)) {
								return ((String) a[index])
										.compareTo(((String) b[index])) > 0 ? -1
										: 1;
							} else if (a[index].getClass().equals(
									BigDecimal.class)) {
								return ((BigDecimal) a[index])
										.compareTo(((BigDecimal) b[index])) > 0 ? -1
										: 1;
							} else if (a[index].getClass().equals(Double.class)) {
								return ((Double) a[index])
										.compareTo(((Double) b[index])) > 0 ? -1
										: 1;
							} else if (a[index].getClass()
									.equals(Boolean.class)) {
								return ((Boolean) a[index])
										.compareTo(((Boolean) b[index])) > 0 ? -1
										: 1;
							} else if (a[index].getClass().equals(Long.class)) {
								return ((Long) a[index])
										.compareTo(((Long) b[index])) > 0 ? -1
										: 1;
							} else if (a[index].getClass().equals(Date.class)) {
								return ((Date) a[index])
										.compareTo(((Date) b[index])) > 0 ? -1
										: 1;
							} else {
								return ((Integer) a[index])
										.compareTo(((Integer) b[index])) > 0 ? -1
										: 1;
							}
						} else {

							if (a[index].getClass().equals(Integer.class)) {
								return ((Integer) a[index])
										.compareTo(((Integer) b[index])) < 0 ? -1
										: 1;
							} else if (a[index].getClass().equals(String.class)) {
								return ((String) a[index])
										.compareTo(((String) b[index])) < 0 ? -1
										: 1;
							} else if (a[index].getClass().equals(
									BigDecimal.class)) {
								return ((BigDecimal) a[index])
										.compareTo(((BigDecimal) b[index])) < 0 ? -1
										: 1;
							} else if (a[index].getClass().equals(Double.class)) {
								return ((Double) a[index])
										.compareTo(((Double) b[index])) < 0 ? -1
										: 1;
							} else if (a[index].getClass()
									.equals(Boolean.class)) {
								return ((Boolean) a[index])
										.compareTo(((Boolean) b[index])) < 0 ? -1
										: 1;
							} else if (a[index].getClass().equals(Long.class)) {
								return ((Long) a[index])
										.compareTo(((Long) b[index])) < 0 ? -1
										: 1;
							} else if (a[index].getClass().equals(Date.class)) {
								return ((Date) a[index])
										.compareTo(((Date) b[index])) < 0 ? -1
										: 1;
							} else {
								return ((Integer) a[index])
										.compareTo(((Integer) b[index])) < 0 ? -1
										: 1;
							}
						}

					}
				});

				tableData.setSortDirection(dir);
				tableData.clearAll();
			}
		};

		for (int i = 0; i < tableData.getColumnCount(); i++) {
			tableData.getColumn(i).addListener(SWT.Selection, sortListener);
		}
		
        setFormMode(AYConst.MODE_ADD);
	}


	protected void btnCancelFormClick(SelectionEvent arg0) {
		resetFormToReadyAdd();
	}

	protected boolean doDeleteFormById() {
		boolean rtrValue = true;

		MessageBox messageBox = new MessageBox(getShell(), SWT.OK | SWT.CANCEL
				| SWT.ICON_WARNING);
		messageBox.setMessage(AYConst.CONFIRM_DELETE);

		if (messageBox.open() == SWT.OK) {
			try {
				doMainDelete();
				Util.OPERATION_CODE = 3;
				Util.showMsgBox(AYConst.TRX_SUCCESS, 1, getShell());
			} catch (Exception e) {
				e.printStackTrace();
				rtrValue = false;
				Util.showMsgBox(
						AYConst.TRX_FAIL + ", karena: " + e.getMessage(), 2,
						getShell());
			}
		}

		return rtrValue;
	}

	public abstract void doClearForm(SelectionEvent arg0);


	private void resetFormToReadyAdd(){
		
		//set Form Key null 
		setFormKey(null);
		
		//Clear form and set mode ADD
		doClearForm(null);

		//enabled control
		enabledControl(true, true);
		
		//disabled button of Form
		disabledButtonOnForm(true);
		
		//set mode form ADD
		setFormMode(AYConst.MODE_ADD);
		
		setFocusForAdd();
	}
	


	private void btnSaveFormClick(SelectionEvent arg0){
		if (!saveData()) {
			return;
		}
		
		//refresh list
		loadDataThread();
		
		//reset form so ready for add new Data 
		resetFormToReadyAdd();
		
	}
	private String formKey;
	
	public String getFormKey() {
		return formKey;
	}


	public void setFormKey(String formKey) {
		this.formKey = formKey;
	}

	
	protected final void prepareFormForEdit() {
		try {
			if (getFormMode() == AYConst.MODE_EDIT) {
				setFormKeyWithSelectedTableItem();	
				getPojoByFormKey(getFormKey());

				// DISABLED KONTROL
				enabledControl(true, false);
				disabledButtonOnForm(true);
			} 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Util.showMsgBox("Gagal load: " + e.getMessage(), 1,
					getShell());
		}
	}
	protected final void disabledButtonOnForm(boolean enabled) {
		//btnSaveForm.setEnabled(!enabled);
		//btnEdit.setEnabled(enabled);
		//btnDeleteForm.setEnabled(enabled);
		//btnCancelForm.setEnabled(false);
		disabledOtherButton(enabled);
	}
	
	
	protected final boolean saveData() {
		if (!doValidate()) {
			return false;
		}

		if (getFormMode() == AYConst.MODE_ADD) {
			if (!doInsert()) {
				return false;
			}
		} else if (getFormMode() == AYConst.MODE_EDIT) {
			if (!doUpdate()) {
				return false;
			}
		}else{
			if (!doInsert()) {
				return false;
			}
		}
		return true;
	}

	protected boolean doInsert() {
		boolean rtrValue = true;

		try {
			int n = doMainInsert();
			if (n < 1) {
				return false;
			}
			Util.OPERATION_CODE = 1;
		} catch (Exception e) {
			e.printStackTrace();

			if (e.getMessage() != null && e.getMessage().contains("Duplicate")) {
				Util.showMsgBox(
						AYConst.TRX_FAIL
								+ ", karena kode/primary key sudah ada, silakan gunakan yang lain!",
						2, getShell());
			} else {
				Util.showMsgBox(
						AYConst.TRX_FAIL + ", karena: " + e.getMessage(), 2,
						getShell());
			}
			rtrValue = false;
		}

		return rtrValue;
	}
	
	protected boolean doUpdate() {
		boolean rtrValue = true;

		try {
			int n = doMainUpdate();
			if (n < 1) {
				return false;
			}
			Util.OPERATION_CODE = 2;
		} catch (Exception e) {
			e.printStackTrace();
			Util.showMsgBox(AYConst.TRX_FAIL + ", karena: " + e.getMessage(),
					2, getShell());
			rtrValue = false;
		}

		return rtrValue;
	}

	public String getFuncId() {
		return funcId;
	}

	public void setFuncId(String funcId) {
		this.funcId = funcId;
	}

	public Object[][] getData() {
		return data;
	}

	public void setData(Object[][] data) {
		this.data = data;
	}

	protected abstract int delete(String key) throws Exception;
	protected abstract int doMainInsert() throws Exception;
	public abstract void getCompositeForm(Composite formContainer);
	protected abstract void enabledControl(boolean otherfield, boolean key);
	public abstract  void setFocusForAdd();
	public abstract  void setFocusForEdit();
	protected abstract void getPojoByFormKey(String key) throws Exception;
	protected abstract void disabledOtherButton(boolean enable);
	public abstract boolean doValidate();
	protected abstract int doMainUpdate() throws Exception;
	protected abstract int doMainDelete() throws Exception;
	abstract protected void setPojoWithFormField() throws Exception;
	abstract protected void fillTable(Object[] obj, TableItem item);
	abstract protected String[] getColumCaption();
	abstract protected int[] getColumWidth();
	abstract protected void populateTable(String sqlDml) throws Exception;
	abstract protected void setFormKeyWithSelectedTableItem();
	abstract public void additionalProcess();
	abstract public void reloadForm();
	
	private void btnClearWidgetSelected(SelectionEvent evt) {
		fSearch.setText("");
		loadDataThread();
	}



	private final void btnReloadWidgetSelected(SelectionEvent evt) {
		loadDataThread();
		doFocusOnTableData();
	}

	private final void btnFindWidgetSelected(SelectionEvent evt) {
		loadDataThread();
		fSearch.setFocus();
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public void setTableData(Table tableData) {
		this.tableData = tableData;
		tableData.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
	}

	public Table getTableData() {
		return tableData;
	}

	private void fSearchKeyReleased(KeyEvent evt) {
		if (evt.keyCode == 13) {
			loadDataThread();
		}
	}

	public final Text getFSearch() {
		return fSearch;
	}

	public void loadSearchData() throws Exception {
		System.out.println("**** LOARD SEARCH DATA ****");
		populateTable("%" + getFSearch().getText() + "%");
	}

	public void loadAllData() throws Exception {
		System.out.println("**** LloadAllData ****");
		populateTable("%%");
	}

	public void loadDataThread() {
		try {
			loadSearchData();
			lblMessage.setText("Jumlah data:" + tableData.getItemCount());
		} catch (Exception e) {
			e.printStackTrace();
			Util.showMsgBox("Gagal load: " + e.getMessage(), 1, getShell());
		}
		fSearch.setFocus();
	}

	public final boolean createTableColumn(Table tableData) {
		tableData.setLinesVisible(true);
		tableData.setHeaderVisible(true);

		boolean rtrValue = true;
		final String[] titles = getColumCaption();
		int[] col = getColumWidth();

		if (titles.length != col.length) {
			System.out
					.println("DATA JUDUL KOLOM DENGAN DATA LEBAR KOLOM TIDAK SAMA");
		}

		if (titles.length == 0) {
			rtrValue = false;
		}

		for (int i = 0; i < titles.length; i++) {
			TableColumn column = new TableColumn(tableData, SWT.NONE);
			column.setText(titles[i]);
		}

		if (col.length == 0) {
			rtrValue = false;
		}

		for (int i = 0; i < col.length; i++) {
			tableData.getColumn(i).setWidth(col[i]);
		}

		tableData.getColumn(0).setResizable(false);

		return rtrValue;
	}

	private final void tableDataKeyPressed(KeyEvent evt) {
		if (evt.keyCode == 13) {
			editSelectedData();
		}
	}

	private final void tableDataMouseDoubleClick(MouseEvent evt) {
		editSelectedData();
	}

	private final void btnNewWidgetSelected(SelectionEvent evt) {
		setFormMode(AYConst.MODE_ADD);
		resetFormToReadyAdd();
	}

	private void doFocusOnTableData() {
		tableData.setFocus();
		if (tableData.getItemCount() > 0 && tableData.getSelectionCount() < 1) {
			tableData.select(0);
		}
	}

	private final void btnEditWidgetSelected(SelectionEvent evt) {
		editSelectedData();
	}

	private void editSelectedData(){
		setFormMode(AYConst.MODE_EDIT);
		prepareFormForEdit();
		setFocusForEdit();
	}
	
	private final void btnDeleteWidgetSelected(SelectionEvent evt) {
/*		if (!Util.isAllowedDelete(getFuncId())) {
			Util.showMsgBox("Anda tidak mempunyai hak untuk hapus data", 1,
					dialogShell);
			return;
		}*/
		deleteData();
		doFocusOnTableData();
	}

	public final String deleteData() {
		TableItem[] items = tableData.getSelection();
		List<String> lst = new ArrayList<String>();
		
		boolean isFormDeleted = false;
		
		if ((items == null) || (items.length < 1)) {
			Util.showMsgBox(AYConst.FAIL_DELETE, AYConst.MSG_WARN, getShell());
		} else {

			try {
				MessageBox messageBox = new MessageBox(getShell(), SWT.OK
						| SWT.CANCEL | SWT.ICON_WARNING);
				messageBox
						.setMessage("Anda Yakin akan menghapus data yang anda pilih?");
				if (messageBox.open() == SWT.OK) {

					for (int j = 0; j < items.length; j++) {
						TableItem itm = (TableItem) items[j];
						String idDelete = itm.getText(0);
						
						//check if current formKey same id will deleted
						if (getFormKey()!=null && idDelete!=null && (idDelete.equals(getFormKey()))){
							isFormDeleted = true;
						}						
						//end of
						
						
						lst.add(idDelete);
					}

					if (lst.size() > 0) {
						deleteList(lst);
						loadDataThread();
					}

				}
			} catch (Exception e) {
				e.printStackTrace();

				if (e.getMessage().contains("FOREIGN KEY")) {
					Util.showMsgBox(
							"Data tidak dapat dihapus karena dipakai oleh table lain, silakan hapus terlebih dulu tabel yang pakai!, "
									+ e.getMessage(), 2, getShell());
				} else {
					Util.showMsgBox("Data yg dipilih gagal dihapus!, karena: "
							+ e.getMessage(), 2, getShell());
				}
			} finally {
				lst = null;
			}
		}

		if (isFormDeleted){
			resetFormToReadyAdd();
		}
		return "success";
	}


	public int getFormMode() {
		return formMode;
	}


	public void setFormMode(int formMode) {
		this.formMode = formMode;
	}
	
	public  int deleteList(List<String> ids) throws Exception  {
		
		int n = 0;
		try {
			SqliteDB.getPersist().setAutoCommit(false);
			for (int j = 0; j < ids.size(); j++) {
				String key = ids.get(j);
				n += delete(key);
			}
			SqliteDB.getPersist().commit();
		} catch (Exception e) {
			e.printStackTrace();
			SqliteDB.getPersist().rollback();
			throw new Exception();
		} finally {
			SqliteDB.getPersist().setAutoCommit(true);
		}
		return n;
	}
}
